using System;
using System.Collections.Generic;
using AdvancedInspector;
using UnityEngine;

[AdvancedInspector(true)]
public class TemporaryProductManager : MonoBehaviour
{
	public static TemporaryProductManager instance;

	public bool timerIsStarted;

	public List<TimingProduct> listTimingProducts = new List<TimingProduct>();

	private string KeyListIdProdcuts = "List_Timing_Product";

	private int sendEventTimeLess = 21600;

	private float timer;

	public static event Action<TimingProduct> timeIsUp;

	public static event Action<TimingProduct> timingProdRemove;

	public static event Action<TimingProduct> timeAlmostIsUp;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		timerIsStarted = true;
		LoadListProduct();
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void LateUpdate()
	{
		if (!timerIsStarted)
		{
			return;
		}
		timer += Time.deltaTime;
		if (!(timer >= 1f))
		{
			return;
		}
		timer -= 1f;
		for (int i = 0; i < listTimingProducts.Count; i++)
		{
			listTimingProducts[i].TimerSec--;
			if (listTimingProducts[i].TimerSec <= 0)
			{
				if (TemporaryProductManager.timeIsUp != null)
				{
					TemporaryProductManager.timeIsUp(listTimingProducts[i]);
				}
			}
			else if (listTimingProducts[i].TimerSec <= sendEventTimeLess && TemporaryProductManager.timeAlmostIsUp != null)
			{
				TemporaryProductManager.timeAlmostIsUp(listTimingProducts[i]);
			}
		}
	}

	public void StartTimer()
	{
		timer = 0f;
		timerIsStarted = true;
	}

	public void AddTimingProduct(string idProd, int timeInSec)
	{
		if (string.IsNullOrEmpty(idProd) || timeInSec <= 0)
		{
			return;
		}
		for (int i = 0; i < listTimingProducts.Count; i++)
		{
			if (listTimingProducts[i].idProd == idProd)
			{
				listTimingProducts[i].TimerSec += timeInSec;
				return;
			}
		}
		listTimingProducts.Add(new TimingProduct(idProd, timeInSec));
	}

	public void CheckTimeIsUpForAllProducts()
	{
		List<TimingProduct> list = new List<TimingProduct>();
		for (int i = 0; i < listTimingProducts.Count; i++)
		{
			if (listTimingProducts[i].TimerSec <= 0)
			{
				list.Add(listTimingProducts[i]);
			}
		}
		for (int j = 0; j < list.Count; j++)
		{
			if (ResetTemporaryProduct(list[j]))
			{
				if (TemporaryProductManager.timingProdRemove != null)
				{
					TemporaryProductManager.timingProdRemove(list[j]);
				}
				listTimingProducts.Remove(list[j]);
			}
		}
		list.Clear();
	}

	private bool ResetTemporaryProduct(TimingProduct curProd)
	{
		return false;
	}

	public void SaveListProduct()
	{
		List<string> list = new List<string>();
		foreach (TimingProduct listTimingProduct in listTimingProducts)
		{
			list.Add(listTimingProduct.idProd);
			listTimingProduct.SaveTimer();
		}
		Save.SaveStringArray(KeyListIdProdcuts, list.ToArray());
	}

	public void LoadListProduct()
	{
		string[] array = Load.LoadStringArray(KeyListIdProdcuts);
		if (array == null)
		{
			return;
		}
		string[] array2 = array;
		foreach (string id in array2)
		{
			TimingProduct timingProduct = new TimingProduct(id);
			timingProduct.LoadTimer();
			if (timingProduct.TimerSec > 0)
			{
				listTimingProducts.Add(timingProduct);
			}
		}
	}

	private void OnApplicationPause(bool pause)
	{
		if (pause)
		{
			SaveListProduct();
		}
	}
}
